package controllers

import (
	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
	httpfacade "github.com/hulutech-web/http_result"
	"goravel/app/http/controllers/common"
	"goravel/app/models"
	"goravel/app/services/Upload"
	"goravel/app/services/excel"
	"strconv"
)

type ExcelController struct {
	//Dependent services
	uploadService *Upload.UploadService
	excelService  *excel.Excelservice
	*common.AuthService
	*common.SearchByParamsService
}

func NewExcelController() *ExcelController {
	return &ExcelController{
		//Inject services
		uploadService: Upload.NewUploadService(),
		excelService:  excel.NewExcelService(),
	}
}

func (r *ExcelController) Upload(ctx http.Context) http.Response {
	file, err := ctx.Request().File("file")
	if err != nil {
		ctx.Request().AbortWithStatus(500)
		return nil
	}
	if att, err1 := r.uploadService.Excelupload(ctx, file); err1 != nil {
		ctx.Request().AbortWithStatus(500)
		return nil
	} else {
		r.excelService.LoadFile(att.ID, att.Path)
		return ctx.Response().Success().Json(http.Json{
			"message": "上传成功",
			"data":    att.ID,
		})
	}
}

func (r *ExcelController) Template(ctx http.Context) http.Response {
	file, err := ctx.Request().File("file")
	if err != nil {
		return httpfacade.NewResult(ctx).Error(http.StatusInternalServerError, "文件上传失败", nil)
	}
	if att, err1 := r.uploadService.ParseUpload(ctx, file); err1 != nil {
		ctx.Request().AbortWithStatus(500)
		return nil
	} else {
		r.excelService.LoadTemplate(att.ID, att.Path)
		return httpfacade.NewResult(ctx).Success("导入成功", nil)
	}
}

func (r *ExcelController) ReadPreview(ctx http.Context) http.Response {
	pageSize := ctx.Request().Query("pageSize", "10")
	pageSizeInt, _ := strconv.Atoi(pageSize)
	order := ctx.Request().Query("order")
	sort := ctx.Request().Query("sort")
	cmds := []models.Readcmd{}
	var total int64
	currentPage := ctx.Request().Query("currentPage", "1")
	currentPageInt, _ := strconv.Atoi(currentPage)
	queries := ctx.Request().Queries()
	if order != "" || sort != "" {
		facades.Orm().Query().Scopes(r.SearchByParams(ctx.Request().Queries())).With("Protorule").Order(sort+" "+order).
			Paginate(currentPageInt, pageSizeInt, &cmds, &total)
	} else {
		facades.Orm().Query().Scopes(r.SearchByParams(queries)).With("Protorule").Order("id desc").
			Paginate(currentPageInt, pageSizeInt, &cmds, &total)
	}

	return ctx.Response().Success().Json(http.Json{
		"data":  cmds,
		"total": total,
		"links": map[string]interface{}{
			"first": "http://" + ctx.Request().Host() + "/api/admin/employee?pageSize=" + pageSize + "&currentPage=1",
			//最后一页应该是总数除以每页的数量取整
			"last": "http://" + ctx.Request().Host() + "/api/admin/employee?pageSize=" + pageSize + "&currentPage=" + strconv.Itoa(int(total)/pageSizeInt),
			"prev": "http://" + ctx.Request().Host() + "/api/admin/employee?pageSize=" + pageSize + "&currentPage=" + strconv.Itoa(currentPageInt-1),
			"next": "http://" + ctx.Request().Host() + "/api/admin/employee?pageSize=" + pageSize + "&currentPage=" + strconv.Itoa(currentPageInt+1),
		},
		"meta": map[string]interface{}{
			"total_page":   int(total) / pageSizeInt,
			"current_page": currentPageInt,
			"per_page":     pageSizeInt,
			"total":        total,
		},
	})
}
